package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.DBProductType;
import net.starwifi.duobao.entity.Gglhd;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.DBProductTypeService;
import net.starwifi.duobao.service.GglhdService;
import net.starwifi.duobao.service.GgljpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller - member.
 *
 * @author ruoo
 * @version 1.0
 */
@Controller("adminProductTypeController")
@RequestMapping("/dbProductType")
public class DBProductTypeController extends BaseController {

    @Autowired
    DBProductTypeService dbProductTypeService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/dbProductType/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(DBProductType dbProductType, RedirectAttributes redirectAttributes) {
        if (!isValid(dbProductType)) {
            return ERROR_VIEW;
        }
        dbProductType.setStatus(Integer.bitCount(0));
        dbProductTypeService.save(dbProductType);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }


    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("entity", dbProductTypeService.find(id));
        return "/dbProductType/edit";
    }


    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(DBProductType dbProductType, RedirectAttributes redirectAttributes) {
        dbProductTypeService.update(dbProductType);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, ModelMap model,HttpServletRequest request) {
        pageView.filterQuery(request);
        pageView.addOrder(Order.desc("orderNum"));
        model.addAttribute("page", dbProductTypeService.getScrollData(pageView));
        return "/dbProductType/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        dbProductTypeService.delete(ids);
        return SUCCESS_MESSAGE;
    }


}
